import os
import numpy as np
import xml.dom.minidom as xmldom

# ======================检查文件夹是否存在====================================
def check_dir(path):
    if not os.path.isdir(path):
        os.makedirs(path)


# ======================读取csv文件====================================
def parse_xxx_csv(path):
    sample = np.loadtxt(path, dtype=np.str)
    return sample


# ======================读取anno信息中的坐标====================================
def parse_anno_xml2loc(path, class_name):
    """
    解析xml 得到object 数组
    :param path:
    :return:
    """
    dom = xmldom.parse(path)
    elm_obj = dom.documentElement
    sub_elm_obj = elm_obj.getElementsByTagName('object')
    li = []
    for obj in sub_elm_obj:
        name = obj.getElementsByTagName('name')[0].childNodes[0].data
        if name != class_name:
            continue
        bndbox = obj.getElementsByTagName('bndbox')[0]
        xmin = bndbox.getElementsByTagName('xmin')[0].childNodes[0].data
        ymin = bndbox.getElementsByTagName('ymin')[0].childNodes[0].data
        xmax = bndbox.getElementsByTagName('xmax')[0].childNodes[0].data
        ymax = bndbox.getElementsByTagName('ymax')[0].childNodes[0].data
        li.append([xmin, ymin, xmax, ymax])
    return li


if __name__ == '__main__':
    li = parse_anno_xml2loc(r'data/VOC_car/train/Annotations/000288.xml', 'person')
    for i in li:
        print(i)